import { _decorator, Component, Node, sys, Game, game } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('StorageUtil')
export class StorageUtil extends Component {
    /**
     * 音效
     */
    public static OnOffSound = "OnOffSound1";
    /**
     * 音乐
     */
    public static OnOffMusic = "OnOffMusic1";
    /**
     * 单利
     */
    public static Instance:StorageUtil = null;
    /**
     * 
     */
    private _localData:object = {};

    onLoad(){
        if(null === StorageUtil.Instance){
            StorageUtil.Instance = this;
        }else{
            this.destroy();
            return;
        }
    }

    public clearAll():void{
        localStorage.clear();
    }

    /**
     * 设置数据
     * @param key 
     * @param value 
     */
    public set(key:string, value:any):void{
        this._localData[key] = value;
        this.setData(key, value);
    }

    /**
     * 获取数据
     * @param key 
     * @param defaultValue 
     */
    public get(key:string, defaultValue:any):any{
        if(this._localData.hasOwnProperty(key)){
            return this._localData[key] || defaultValue;
        }else{
            var tmp = this.getData(key, defaultValue);
            this._localData[key] = tmp;
            return tmp;
        }
    }

    /**
     * 清除数据
     * @param key 
     */
    public clear(key:string):void{
        delete this._localData[key];
        localStorage.removeItem(key);
    }

    /**
     * 存储 string 类型数据
     * @param key 
     * @param value 
     */
    public setString(key:string, value:string):void{
        try {
            localStorage.setItem(key, value);
        } catch (e) {
            console.error(key, "[StorageUtil] setString set Local Storage fail");
        }
    }

    /**
     * 读取 string 类型数据
     * @param key 
     * @param defaultValue 
     * @returns 
     */
    public getString(key:string, defaultValue:string=""):string{
        try {
            var tmp = localStorage.getItem(key);
            return tmp ? tmp : defaultValue;
        } catch (e) {
            return defaultValue;
        }
    }

    /**
     * 基础api: 设置（存储）数据
     * @param key 键名
     * @param value 值
     */
    public setData(key:string, value:any):void{
        try {
            var data:string = value && JSON.stringify(value);
            localStorage.setItem(key, data);
        } catch (e) {
            console.error(key, "[StorageUtil] setData set Local Storage fail");
        }
    }

    /**
     * 基础api：获取数据
     * @param key 键名
     * @param defaultValue 默认值
     * @returns 
     */
    public getData(key:string, defaultValue:any):any{
        try {
            var tmp = localStorage.getItem(key);
            return tmp?JSON.parse(tmp) : defaultValue;
        } catch (e) {
            return defaultValue;
        }
    }

    ////////////////////////////////////////////////////////////////////////////////
    public getOnOffMusic(): boolean {
        //
        if("1" == this.getString(StorageUtil.OnOffMusic, "1")){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 保存音效状态
     * @param value 
     */
    public setOnOffMusic(value:boolean):void{
        if(value){
            this.setString(StorageUtil.OnOffMusic, "1");
        }else{
            this.setString(StorageUtil.OnOffMusic, "0");
        }
    }

    /**
     * 获取音效开关状态，true 音效打开中，false 音效关闭
     * @returns 
     */
    public getOnOffSound():boolean{
        //
        if("1" == this.getString(StorageUtil.OnOffSound, "1")){
            return true;
        }else{
            return false;
        }
    }
    /**
     * 保存音效状态
     * @param value 
     */
    public setOnOffSound(value:boolean):void{
        if(value){
            this.setString(StorageUtil.OnOffSound, "1");
        }else{
            this.setString(StorageUtil.OnOffSound, "0");
        }
    }
}

